<template>
    <div>
        <br>
        <el-table
                :data="fileList"
                style="width: 100%">
            <el-table-column
                    prop="goodId"
                    label="id"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="name"
                    label="商品名"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="integral"
                    label="所需积分"
                    width="180">
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-popconfirm title="确定兑换该文件吗？" @confirm="handleDelete(scope.row)">
                        <el-button
                                slot="reference"
                                type="primary"
                                size="mini">兑换</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script>
export default {
    name: "file",
    data () {
        return {
            form:{
                name:'',
                integral:''
            },
            open: false,
            uploadUrl:'',
            fileList:[],
            show: false
        }
    },
    created() {
        this.getFileList();
    },
    methods:{
        addFile(){
            let vm = this;
            this.axios({
                url: '/myapi/admin/integralGood/saveOrUpdate',
                method: 'post',
                data:{
                    integral: vm.form.integral,
                    name: vm.form.name
                }
            }).then(function (response) {
                if(response.data.success === true){
                    vm.$message({
                        message: '添加成功!',
                        type: 'success'
                    });
                    vm.getFileList();
                    vm.open = false;
                }
                else {
                    vm.$message.error('失败');
                }
            });
        },
        handleUploadForm(param) {
            console.log(this.form.type);
            let thisInfo = this
            let formData = new FormData()
            // 在formData中加入我们需要的参数
            formData.append('file', param.file)
            formData.append('type', this.form.type)

            // 向后端发送数据
            thisInfo.$axios.post('/myapi/Qiniu/upload', formData).then((res) => {
                if (res.data.success === true) {
                    thisInfo.$message.success('文件上传成功');
                    thisInfo.open = false;
                    thisInfo.getFileList();
                } else {
                    thisInfo.$message.success('文件上传失败')
                }
                thisInfo.formFileList = []
                thisInfo.uploadFormFileList = []
            })
        },

        beforeUploadHandle(file) {
            /*            if (file.type !== "pdf") {
                            this.$message.error("只支持pdf文件！");
                            return false;
                        }
                        return true;*/
        },

        handleDownload(row){
            let url = row.fileUrl;
            window.open("http://"+url);
            /*			console.log("url---------------"+url);
                        const ele = document.createElement('a');
                        ele.setAttribute('href', "http://"+url);
                        ele.setAttribute('download',"val.name");
                        ele.style.display = 'none';
                        document.body.appendChild(ele);
                        ele.click();
                        document.body.removeChild(ele);*/

        },
        handleDelete(row){
            let vm = this;
            this.axios({
                url: '/myapi/user/integralGood/exchange/'+vm.$store.state.id+'/'+row.goodId,
                method: 'get'
            }).then(function (response) {
                if(response.data.success === true){
                    vm.$message({
                        message: '兑换成功!',
                        type: 'success'
                    });
                    vm.getFileList();
                }
                else {
                    vm.$message.error('积分不足');
                }
            });
        },
        getFileList(){
            let vm = this;
            this.axios({
                url: '/myapi/user/integralGood/list',
                method: 'get'
            }).then(function (response) {
                if(response.data.success === true){
                    vm.fileList = response.data.content;
                    console.log(vm.fileList);
                }
                else {
                    vm.$message.error('获取列表失败');
                }
            });
        }
    }
}
</script>

<style scoped>

</style>
